package com.eryos.gwt.androidmarketinformation;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gc.android.market.api.MarketSession;
import com.gc.android.market.api.MarketSession.Callback;
import com.gc.android.market.api.model.Market.App;
import com.gc.android.market.api.model.Market.AppsRequest;
import com.gc.android.market.api.model.Market.AppsResponse;
import com.gc.android.market.api.model.Market.ResponseContext;

@SuppressWarnings("serial")
public class AndroidMarketInformationServlet extends HttpServlet {

	private String version = "2";
 
	/* Retrieve info from Param File */
	private String mail 		= Messages.getString("AndroidMarketInformationServlet.mail"); 
	private String pass 		= Messages.getString("AndroidMarketInformationServlet.pass"); 
	private String androidID 	= Messages.getString("AndroidMarketInformationServlet.androidID"); 

	/* Current ServletResponse */
	private HttpServletResponse currentResp;
	
	public void doGet(HttpServletRequest req, final HttpServletResponse resp) throws IOException {
		currentResp = resp;
		resp.setContentType("text/plain"); 
		resp.getWriter().println("Hello, world : "+version); 

		String query = ""; 
		if (req.getParameterMap().containsKey("appli")){  

			/* Retrieve params from URL */
			query = req.getParameter("appli"); 
			resp.getWriter().println("appli : "+query); 
			
			try {
				/* Open a market session */
				MarketSession session = new MarketSession();  
				session.login(mail, pass); 
				session.getContext().setAndroidId(androidID);     

				/* Build a search request */
				AppsRequest appsRequest = AppsRequest.newBuilder().setQuery(query).setStartIndex(0).setEntriesCount(10).setWithExtendedInfo(true).build();

				/* Send the request */
				session.append(appsRequest, new MarketCallback());
				
				session.flush();
			} catch (Exception e) {
				resp.getWriter().println("ERROR : "+e.getMessage());
				e.printStackTrace();
			}
		}else{
			resp.getWriter().println("No 'appli' defined in this request"); 
		}
	}

	
	public class MarketCallback implements Callback<AppsResponse>{

		@Override
		public void onResult(ResponseContext arg0, AppsResponse response) {
			try {
				currentResp.getWriter().println("Nb:"+response.getAppCount()); 
				currentResp.getWriter().println("-------------------------------"); 
				for ( App apps : response.getAppList() ){
					currentResp.getWriter().println("Title:"+apps.getTitle()); 
					currentResp.getWriter().println("Package:"+apps.getPackageName()); 
					currentResp.getWriter().println("Version:"+apps.getVersion()); 
					currentResp.getWriter().println("VersionCode:"+apps.getVersionCode()); 
					currentResp.getWriter().println("ID:"+apps.getId()); 
					currentResp.getWriter().println("-------------------------------"); 
				}
			} catch (IOException e) { 
				e.printStackTrace();
			}	                
		}
	}
}
